package com.atguigu.day26.problem03;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/3 16:35
 * @注释
 */
public class DruidTest {
    public class JDBCUtils {

        public static void main(String[] args) {
            Connection connection = getConnection();
            System.out.println("connection = " + connection);
        }
        /**
         * 通过连接池对象获取连接
         * @return
         */
        public static Connection getConnection() {

            Properties properties = new Properties();
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            try {
                properties.load(is);
                properties.list(System.out);
                DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
                return dataSource.getConnection();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*@Test
    public void test01() throws Exception {
        //创建连接池对象
        DruidDataSource ds = new DruidDataSource();
        //驱动名称
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql:///atguigu");
        ds.setUsername("root");
        ds.setPassword("1234");
        //初始化连接数
        ds.setInitialSize(5);
        //设置最大连接数
        ds.setMaxActive(10);
        //设置最大等待时间
        ds.setMaxWait(5000);
        for (int i = 1; i <= 10; i++) {
            DruidPooledConnection connection = ds.getConnection();
            System.out.println(i+"counection = " + connection);
            connection.close();
        }
    }
*/

}
